﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using SeAssignment.Models;
using BusinessLayer;
using System.Net.Mail;

namespace SeAssignment.Controllers
{
    public class ProductController : Controller
    {
        public ActionResult Details(int id)
        {
            Product p = new ProductBL().GetProductByID(id);
            ProductDetailModel pd = new ProductDetailModel();
            pd.ID = p.ID;
            pd.Name = p.Name;
            pd.Price = (double)p.Price;
            pd.StockAvailable = (int)p.StockAmount;
            pd.Features = p.Description;
            pd.Image = p.ImageURL;

            return View("Details", pd);
        }

        [HttpPost]
        public void AddProductsToCart(int productID)
        {
            new ProductBL().AddToCart(HttpContext.User.Identity.Name, productID, 1);
        }

        public ActionResult ShoppingCart()
        {
            List<CartModel> Cart = new List<CartModel>();
            CartModel ShoppingCart;

            foreach (Common.Cart c in new ProductBL().GetItemsInShoppingCart(HttpContext.User.Identity.Name))
            {
                ShoppingCart = new Models.CartModel();
                Product p = new ProductBL().GetProductByID((int)c.ProductID);
                ShoppingCart.CartID = c.ID;
                ShoppingCart.ProductID = p.ID;
                ShoppingCart.ProductName = p.Name;
                ShoppingCart.Quantity = (int)c.Quantity;
                ShoppingCart.Image = p.ImageURL;
                Cart.Add(ShoppingCart);
            }
            return View("ShoppingCart", Cart);
        }

        public int NoOfItemInShopppingCart()
        {
            return new ProductBL().GetNoOfItemsInShoppingCart(HttpContext.User.Identity.Name);
        }

        public ActionResult PlaceOrder()
        {

            Common.Order O = new Order();
            O.OrderID = Guid.NewGuid();
            O.Date = DateTime.Now;
            O.UserName = HttpContext.User.Identity.Name;


            new ProductBL().AddOrder(O);

            foreach (Cart c in new ProductBL().GetItemsInShoppingCart(HttpContext.User.Identity.Name))
            {
                OrderDetail p = new OrderDetail();
                p.ProductID = (int)c.ProductID;
                p.Quantity = (int)c.Quantity;
                p.OrderID = O.OrderID;

                Product P = new ProductBL().GetProductByID((int)c.ProductID);
                P.StockAmount = P.StockAmount - c.Quantity;

                new ProductBL().AddOrderDetails(p);
                new ProductBL().UpdateProduct(P);
                new ProductBL().DeleteCart(c.ID);
            }

            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("andreaconsiglio2@gmail.com");
                mail.To.Add(new UsersBL().GetUserByUserName(O.UserName).Email);
                mail.Subject = "Order Invoice";
                mail.Body = "Dear " + O.UserName + "\n This Email is being sent to infrom you that an order has been placed to " + O.User.HouseNo + ", "
                    + O.User.StreetName + ", " + O.User.Town.TownName + "\n Order ID is :" + O.OrderID;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("andreaconsiglio2@gmail.com", "Andrea79904782");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

            }
            catch (Exception)
            {
                ModelState.AddModelError("", "NO internet Connection Dummy");
            }

            List<Product> Products = new ProductBL().GetAllProducts().ToList();
            return RedirectToAction("HomePage", "Home", Products);
        }

        public ActionResult RemoveProductFromCart(int cartID)
        {
            new ProductBL().RemoveProductFromCart(cartID);

            if (NoOfItemInShopppingCart() == 0)
                return RedirectToAction("HomePage", new ProductBL().GetAllProducts());
            else
                return ShoppingCart();
        }

    }
}
